// pages/manage/manageStudent/manageStudent.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    major: null,
    grade: null,
    id: null,
    headerArray: [{
      text: '学号',
    }, {
      text: '姓名'
    }, {
      text: '专业'
    }, {
      text: '班级'
    }, {
      text: '手机号'
    },{
      text: '操作'
    }],
    userList: [],
    user: null
  },

  inputMajor(e) {
    this.setData({
      major: e.detail.value
    })
  },

  inputGrade(e) {
    this.setData({
      grade: e.detail.value
    })
  },

  inputId(e) {
    this.setData({
      id: e.detail.value
    })
  },

  findByMajor() {
    var that = this;
    wx.request({
      url: 'http://localhost:8080/clock_war_exploded/user/findByMajor',
      method: "POST",
      data: {
        major: this.data.major
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        if (res.data != null) {
          console.log(res.data)
          that.setData({
            searchLoading: true, //把"上拉加载"的变量设为true，显示  
            searchLoadingComplete: false, //把“没有数据”设为false，隐藏 
            userList: res.data
          })
        } else {
          that.setData({
            searchLoadingComplete: true, //把“没有数据”设为true，显示  
            searchLoading: false //把"上拉加载"的变量设为false，隐藏  
          })
        }
      }
    })
  },

  findByGrade() {
    var that = this;
    wx.request({
      url: 'http://localhost:8080/clock_war_exploded/user/findByGrade',
      method: "POST",
      data: {
        major: this.data.major,
        grade: this.data.grade
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        if (res.data != null) {
          console.log(res.data)
          that.setData({
            searchLoading: true, //把"上拉加载"的变量设为true，显示  
            searchLoadingComplete: false, //把“没有数据”设为false，隐藏 
            userList: res.data
          })
        } else {
          that.setData({
            searchLoadingComplete: true, //把“没有数据”设为true，显示  
            searchLoading: false //把"上拉加载"的变量设为false，隐藏  
          })
        }
      }
    })
  },

  findById() {
    var that = this;
    wx.request({
      url: 'http://localhost:8080/clock_war_exploded/user/findById',
      method: "POST",
      data: {
        id: this.data.id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        if (res.data != '') {
            that.setData({
              searchLoading: true, //把"上拉加载"的变量设为true，显示  
              searchLoadingComplete: false, //把“没有数据”设为false，隐藏 
              userList: [res.data]
            })
        } else {
          that.setData({
            searchLoadingComplete: true, //把“没有数据”设为true，显示  
            searchLoading: false, //把"上拉加载"的变量设为false，隐藏  
            userList:[]
          })
        }
      }
    })
  },

  click: function(e) {
    var that = this;
    let id= e.currentTarget.dataset.id;
    wx.request({
      url: 'http://localhost:8080/clock_war_exploded/manager/deleteUser',
      method:'POST',
      header:{'content-type':'application/x-www-form-urlencoded;charset=utf-8'},
      data: {
        id: id
      },
      success: function (res) {
        if (res.data){
        wx.showToast({
          title: '提交成功',
          icon:'success',
        })
        if(that.data.major != null && that.data.grade == null){
          that.findByMajor()
        }else if(that.data.major != null && that.data.grade != null){
          that.findByGrade()
        }else{
          that.findById()
        }
        }else{
        wx.showModal({
          title: '温馨提示',
          content: '您已提交过，请不要重复提交',
        })
        }       
      }
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})